package com.jet.multichat.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

//TODO:[au] write a contract to the class
public class Message {
  
	//private String from;
    //FIXME:[au] uncommunicative name!
	private String to;
	private String text;

	//TODO:[au] write a contract to the constructor
	public Message(BufferedReader in) throws IOException {
		String inMessage = (String) in.readLine();
		if (inMessage.startsWith("[")) {
			String[] msg = inMessage.split("]");
			this.to = msg[0].substring(1,msg[0].length());
			this.text = msg[1];
		} else {
			this.to = "all";
			this.text = inMessage;
		}
	}

	public Message(String to, String text) {
		this.to = to;
		this.text = text;
	}

	public String getTo() {
		return to;
	}

	public String getText() {
		return text;
	}

	public void sendMessage(PrintWriter out) {
		out.println(this.text);
		out.flush();
	}
	
	public void show() {
		System.out.println(text);
	}

	@Override
	public String toString() {
		return text;
	}
}
